package controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import systems.RecentlyDomain;

import models.CheckSpeedModel;
import models.DomainAnalyticModel;
import models.DomainModel;
import models.GoogleAdsenceModel;
import models.GoogleAnalyticModel;
import models.InLinkModel;
import models.IpAddressModel;
import models.OwnerDomainModel;

import entities.CheckSpeed;
import entities.Domain;
import entities.DomainAnalytic;
import entities.GoogleAdsense;
import entities.GoogleAnalytic;
import entities.InLink;
import entities.IpAddress;
import entities.OwnerDomain;

public class DomainPreviewServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (name == null || name.isEmpty()) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		DomainModel domainModel = DomainModel.getInstance();
		Domain domain = domainModel.get(name);
		
		if (domain == null) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		//Get owner data
		OwnerDomainModel ownerDomainModer = OwnerDomainModel.getInstance();
		OwnerDomain ownerDomain = ownerDomainModer.getByDomain(name);
		
		if (ownerDomain == null) {
			response.sendRedirect("/info/" + name);
			return;
		}
		
		//Get google analytic
		GoogleAnalyticModel googleAnalyticData = GoogleAnalyticModel.getInstance();
		GoogleAnalytic googleAnalytic = googleAnalyticData.get(domain.getGoogleAnalyticId());
		
		//Get google adsence
		GoogleAdsenceModel googleAdsenceData = GoogleAdsenceModel.getInstance();
		GoogleAdsense googleAdsense = googleAdsenceData.get(domain.getGoogleAdsenseId());
		
		//Get outlink
		InLinkModel oData = InLinkModel.getInstance();
		InLink inlink = oData.get(domain.getDomain());
		
		//Get domain analytic
		DomainAnalyticModel domainAnalyticData = DomainAnalyticModel.getInstance();
		DomainAnalytic domainAnalytic = domainAnalyticData.get(name);
		
		//Get same domain ip address
		IpAddressModel ipAddressModel = IpAddressModel.getInstance();
		IpAddress ipAddress = null;
		if (!domain.getIpAddress().isEmpty()) {
			ipAddress = ipAddressModel.get(domain.getIpAddress());
		}
		
		//Get checkspeed domain
		CheckSpeedModel checkSpeedModel = CheckSpeedModel.getInstance();
		CheckSpeed checkSpeed = checkSpeedModel.get(domain.getDomain());
		
		request.setAttribute("domain", domain);
		request.setAttribute("ownerDomain", ownerDomain);
		request.setAttribute("domainAnalytic", domainAnalytic);
		request.setAttribute("googleAnalytic", googleAnalytic);
		request.setAttribute("googleAdsense", googleAdsense);
		request.setAttribute("inlink", inlink);
		request.setAttribute("ipAddress", ipAddress);
		request.setAttribute("checkSpeed", checkSpeed);
		try {
			request.getRequestDispatcher("/pages/info.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
